/*
 * @Author: malgee
 * @Date: 2025-04-23 13:58:16
 * @LastEditors: maling
 * @LastEditTime: 2025-04-28 08:46:59
 * @FilePath: /vue3-ts-cms/src/store/main/system/ststem.ts
 * @Description: 
 * 
 */
import { Module } from "vuex";
import { IRootState } from "@/store/types";
import { ISystemState } from "./types";
import { fetchPageList } from "@/service/main/system/system";

export default {
  namespaced: true,
  state: {
    userList: [],
    userCount: 0
  },
  mutations: {
    setUserList(state, userList: any[]) {
      state.userList = userList
    },
    setUserCount(state, userCount: number) {
      state.userCount = userCount
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1. 获取 pageUrl
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`;
      console.log('>>><<<',pageUrl, payload.queryInfo);
      
      const res = await fetchPageList(pageUrl, payload.queryInfo)
      
      console.log('>>><<< === ',res.data);
      
      // 转换 pageName 驼峰
      const changePageName = pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`set${changePageName}List`, res.data)
      commit(`set${changePageName}Count`, res.data.count)
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]  // 获取list数据 eg: state["userList"]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]  // 获取count数据 eg: state["userCount"]
      }
    }
  }
} as Module<ISystemState, IRootState>